package com.github.east196.terminator.xtend.meta;

import com.github.east196.terminator.xtend.model.Field;
import com.github.east196.terminator.xtend.model.Project;
import com.github.east196.terminator.xtend.model.Record;
import com.github.east196.terminator.xtend.model.Three;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Generated;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.Range;
import org.apache.poi.hwpf.usermodel.Table;
import org.apache.poi.hwpf.usermodel.TableIterator;
import org.apache.poi.hwpf.usermodel.TableRow;
import org.eclipse.xtext.xbase.lib.CollectionLiterals;
import org.eclipse.xtext.xbase.lib.Exceptions;
import org.eclipse.xtext.xbase.lib.InputOutput;
import org.eclipse.xtext.xbase.lib.StringExtensions;

@SuppressWarnings("all")
@Generated("org.eclipse.xtend.core.compiler.XtendGenerator")
public class DocMetaParser implements MetaParser {
  @Override
  public List<Three> action(final String docx) {
    ArrayList<Three> _xblockexpression = null;
    {
      final ArrayList<Table> tables = DocMetaParser.tables(docx);
      final Table projectTable = tables.get(0);
      final TableRow projectRow = projectTable.getRow(3);
      Project project = new Project();
      project.setVersion(projectRow.getCell(0).text().trim());
      project.setName(projectRow.getCell(1).text().trim());
      project.setLabel(projectRow.getCell(2).text().trim());
      project.setPath(projectRow.getCell(3).text().trim());
      project.setRoot(projectRow.getCell(4).text().trim());
      project.setPort(projectRow.getCell(5).text().trim());
      final TableRow webRow = projectTable.getRow(4);
      project.setWebPath(webRow.getCell(3).text().trim());
      project.setWebRoot(webRow.getCell(4).text().trim());
      InputOutput.<Project>println(project);
      final ArrayList<Three> threes = CollectionLiterals.<Three>newArrayList();
      for (int i = 1; (i < tables.size()); i++) {
        {
          Table table = tables.get(i);
          Record record = new Record();
          final TableRow recordOkRow = table.getRow(0);
          record.setGeneOk(recordOkRow.getCell(1).text().trim());
          final TableRow recordRow = table.getRow(3);
          record.setProjectId(project.getId());
          record.setDbType(recordRow.getCell(0).text().trim());
          record.setName(StringExtensions.toFirstLower(recordRow.getCell(1).text().trim()));
          record.setLabel(recordRow.getCell(2).text().trim());
          record.setDoc(recordRow.getCell(3).text().trim());
          InputOutput.<Record>println(record);
          final ArrayList<Field> fields = CollectionLiterals.<Field>newArrayList();
          for (int rowIndex = 6; (rowIndex < table.numRows()); rowIndex++) {
            {
              TableRow fieldRow = table.getRow(rowIndex);
              InputOutput.<TableRow>print(fieldRow);
              Field field = new Field();
              field.setProjectId(project.getId());
              field.setRecordId(record.getId());
              field.setType(fieldRow.getCell(0).text().trim());
              field.setName(fieldRow.getCell(1).text().trim());
              field.setLabel(fieldRow.getCell(2).text().trim());
              field.setDoc(fieldRow.getCell(3).text().trim());
              field.setRequired(fieldRow.getCell(4).text().trim());
              field.setKeyType(fieldRow.getCell(5).text().trim());
              field.setSortIndex(fieldRow.getCell(6).text().trim());
              field.setShow(fieldRow.getCell(7).text().trim());
              boolean _isNullOrEmpty = StringExtensions.isNullOrEmpty(field.getValid());
              if (_isNullOrEmpty) {
                field.setValid("{}");
              }
              InputOutput.<Field>println(field);
              fields.add(field);
            }
          }
          final Three three = new Three(project, record, fields);
          threes.add(three);
        }
      }
      _xblockexpression = threes;
    }
    return _xblockexpression;
  }
  
  public static HWPFDocument doc(final String path) {
    try {
      HWPFDocument _xblockexpression = null;
      {
        FileInputStream is = new FileInputStream(path);
        _xblockexpression = new HWPFDocument(is);
      }
      return _xblockexpression;
    } catch (Throwable _e) {
      throw Exceptions.sneakyThrow(_e);
    }
  }
  
  public static ArrayList<Table> tables(final String path) {
    return DocMetaParser.tables(DocMetaParser.doc(path));
  }
  
  public static ArrayList<Table> tables(final InputStream is) {
    try {
      return DocMetaParser.tables(new HWPFDocument(is));
    } catch (Throwable _e) {
      throw Exceptions.sneakyThrow(_e);
    }
  }
  
  public static ArrayList<Table> tables(final HWPFDocument doc) {
    ArrayList<Table> _xblockexpression = null;
    {
      Range range = doc.getRange();
      final ArrayList<Table> tables = CollectionLiterals.<Table>newArrayList();
      TableIterator tableIterator = new TableIterator(range);
      while (tableIterator.hasNext()) {
        {
          Table table = tableIterator.next();
          tables.add(table);
        }
      }
      _xblockexpression = tables;
    }
    return _xblockexpression;
  }
}
